package com.sudo.service.admin.task;

import com.sudo.common.utils.JsonUtil;
import com.sudo.dao.admin.model.vo.SysMemCpuVO;
import com.sudo.rpc.api.model.CallTask;
import com.sudo.rpc.api.utils.RpcRes;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Component
public class AdminTaskModuleCallBack extends AdminBaseService {
    @Resource
    private SysInfoService sysInfoService;

    public void insertMemCpuToRedis(Integer jobId, CallTask callTask, RpcRes rpcRes) {
        log.info("insertMemCpuToRedis被回调，" +
                "参数JobId：{}，IP：{}，port：{}，beanName：{}，methodName：{}",
                jobId,callTask.getIp(),callTask.getPort(),callTask.getBeanName(),callTask.getMethodName());
        String server = callTask.getIp() + ":" + callTask.getPort();
        Map<String, Object> objectMap = JsonUtil.jsonToPojo(JsonUtil.objectToJson(rpcRes.getData()), Map.class);
        SysMemCpuVO sysMemCpuVO = map2MemCpu(objectMap);
        sysInfoService.insertMemCpuToRedis(server,sysMemCpuVO);
    }

    public void insertSysInfoByBatch(Integer jobId, CallTask callTask, RpcRes rpcRes) {
        log.info("insertSysInfoByBatch被回调，" +
                        "参数JobId：{}，IP：{}，port：{}，beanName：{}，methodName：{}",
                jobId,callTask.getIp(),callTask.getPort(),callTask.getBeanName(),callTask.getMethodName());
        String server = callTask.getIp() + ":" + callTask.getPort();
        Map<String, Object> objectMap = JsonUtil.jsonToPojo(JsonUtil.objectToJson(rpcRes.getData()), Map.class);
        sysInfoService.deleteSysInfoByServer(server);
        sysInfoService.insertSysInfoByBatch(server, objectMap);
    }
}
